/**
 * Created by oneminute on 2014/4/24.
 *
 * 该模块为全局配置模块。程序中用到的所有可配置变量均应配置到该模块的合适位置中。
 *
 */

var moment = require("moment");
var winston = require("winston");
var mqTransport = require("../lib/utils/MessageQueueTransport");

var servers = require("./servers_config");
var errors = require("./errors_config");
var routes = require("./routes_config");
var logs = require("./logs_config");

var rabbitServerConfig = require("./rabbit_config");

/**
 * 应用配置对象。
 *
 * @type {{version: string, forceInit: boolean, datetimeFormat: string, datetimeFormatLong: string, business: {itemsPerUser: number, searchRadius: number, searchLimit: number, needExpire: number}, logOptions: {transports: *[]}, webServer: {port: number, timeout: number, page: number}, rabbit: {address: string}, resource: {imageHost: string, aliyun: {oss: {accessKeyId: string, accessKeySecret: string, host: string}, bucket: string}}, db: {redis: {port: number, host: string}, arango: {url: string, database: string, hostname: string, users: string, commodities: string, demands: string, gifts: string, emails: string, accounts: string, issues: string, messages: string, expire: number, aqls: {GET_USER_BY_ID: string, GET_OBJECT_BY_ID: string, GET_USER_BY_NAME: string, GET_OBJECT_BY_PROPERTY: string, GET_OFFLINE_MESSAGES: string, CHECK_USERNAME: string, GET_USER_BY_EMAIL: string, GET_USERS_BY_GEO: string}}}, errors: {SYSTEM_ERROR: {code: number, message: string, err: null}, USER_HAS_BEEN_REMOVED: {code: number, message: string, err: null}, LOGIN_USER_NOT_FOUND: {code: number, message: string, err: null}, LOGIN_WRONG_PASSWORD: {code: number, message: string, err: null}, LOGIN_ALREADY_ONLINE: {code: number, message: string, err: null}, REGISTER_USER_EXISTED: {code: number, message: string, err: null}, REGISTER_EMAIL_EXISTED: {code: number, message: string, err: null}, REGISTER_NO_EMAIL: {code: number, message: string, err: null}, VALIDATE_PROPERTY_NOT_FOUND: {code: number, message: string, err: null}, VALIDATE_INVALIDATE_VALUE: {code: number, message: string, err: null}, ARANGODB_SYSTEM_ERROR: {code: number, message: string, err: null}, REQUEST_ERROR: {code: number, message: string, err: null}, REQUEST_TEMPUSER_ERROR: {code: number, message: string, err: null}, REQUEST_MISS_VALUE: {code: number, message: string, err: null}, REQUEST_DATA_NOT_BELANG_USER: {code: number, message: string, err: null}, REQUEST_NO_COMMODITY: {code: number, message: string, err: null}, REQUEST_NO_DEMAND: {code: number, message: string, err: null}, REQUEST_NO_GIFT: {code: number, message: string, err: null}, REQUEST_SHOP_NOT_IN_BUSINESS: {code: number, message: string, err: null}, REQUEST_NO_DEVICE: {code: number, message: string, err: null}, IMAGE_ERROR: {code: number, message: string, err: null}, getError: getError}, regexps: {email: RegExp, datetime_short: RegExp, datetime_long: RegExp}, devices: {android: Array, ios: Array, web: Array}, routes: {basePath: string, filters: {name: string, module: string}[], paths: *[]}}}
 */
var config = {
    /**
     * 当前应用版本号。
     */
    version: "0.3.022",
    debug: false,
    /**
     * 是否在服务器启动时进行强制数据初始化。若该值为true，所每次启动都会清空整个数据库，填入初始测试数据。
     */
    forceInit: false,
    /**
     * 程序中使用的日期短格式。
     */
    datetimeFormat: "YYYY-MM-DD HH:mm:ss",
    /**
     * 程序中使用的日期长格式。
     */
    datetimeFormatLong: "YYYY-MM-DD HH:mm:ss:SSS",
    /**
     * 业务配置数据对象。
     */
    business: {
        /**
         * 每个用户店铺可被检索的商品数。
         */
        itemsPerUser: 3,
        /**
         * 周边搜索的查询半径，以米为单位。
         */
        searchRadius: 5000000,
        /**
         * 搜索限制.
         */
        searchLimit: 33,
        /**
         * 需求过期天数.
         */
        needExpire: 7
    },
    /**
     * 短信平台配置信息。
     */
    sms: {
        /**
         * 过期时长，以秒为单位。
         */
        expire: 600,
        /**
         * 重发消息时间间隔，以秒为单位。
         */
        resendinterval: 60,
        /**
         * 短信平台用户名。
         */
        account: "cf_oneminute",
        /**
         * 短信平台密码。
         */
        password: "830428lx",
        /**
         * 短信平台连接字符串，请使用require("util").format(lz.Config.sms.url, xx, xx...)来生成实际字符串。
         */
        url: "http://106.ihuyi.cn/webservice/sms.php?method=Submit&account=%s&password=%s&mobile=%s&content=%s",
        /**
         * 短信平台消息模板。
         */
        templates: {
            register: "您的验证码是：【%s】。请不要把验证码泄露给其他人。如非本人操作，可不用理会！",
            findpassword: "您在“我要商城”的登陆密码为【%s】。请您在收到该短信后即时登陆修改密码，并将此短信删除。"
        }
    },
    logOptions: logs,
    webServer: {
        port: 3000,
        timeout: 10000,
        page: 30,
        descLength: 128
    },
    rabbit: rabbitServerConfig,
    resource: {
        aliyun: {
            oss: {
                accessKeyId: 'JqoFlhMxrDyWk5tR',
                accessKeySecret: '3CSuqwwnIXjcR98OkGnLdp4wzmZdiP',
                host: 'oss-cn-qingdao.aliyuncs.com'
            },
            bucket: 'iwant-images'
        }
    },
    db: {
        redis: {
            port: 6379,
            host: '127.0.0.1'
        },
        arango: {
            database: "iwant",
            hostname: servers.arangodb.host,
            username: servers.arangodb.username,
            password: servers.arangodb.password,
            users: "USERS_COLLECTION",
            commodities: "COMMODITIES_COLLECTION",
            demands: "DEMANDS_COLLECTION",
            gifts: "GIFTS_COLLECTION",
            emails: "EMAILS_COLLECTION",
            accounts: "ACCOUNTS_COLLECTION",
            issues: "ISSUES_COLLECTION",
            messages: "MESSAGES_COLLECTION",
            codes: "CODES_COLLECTION",
            records: "RECORDS_COLLECTION",
            expire: 1800,
            aqls: {
                GET_USER_BY_ID: "FOR i IN @@collection FILTER i.`id` == @uid RETURN i",
                GET_OBJECT_BY_ID: "FOR i IN @@collection FILTER i.`id` == @oid RETURN i",
                GET_USER_BY_NAME: "FOR u IN @@collection FILTER u.`name` == @name RETURN u",
                GET_OBJECT_BY_PROPERTY: "FOR obj IN @@collection FILTER obj.@prop == @value RETURN obj",
                GET_OFFLINE_MESSAGES: "FOR m IN @@collection FILTER m.header.recieverid == @value SORT m.header.timestamp RETURN m",
                CHECK_USERNAME: "FOR u IN @@collection FILTER u.name == @name RETURN u",
                GET_USER_BY_EMAIL: "FOR u IN @@users FOR e IN @@emails FILTER u.id == e.uid && e.email == @email RETURN u",
                GET_USERS_BY_GEO: "FOR u IN WITHIN(@@users, @lat, @lon, @radius, 'distance') FILTER u.inbusiness == true && LENGTH(u.@prop) > 0 LIMIT @start, @count SORT u.distance RETURN {_id:u._id,id:u.id,name:u.name,distance:ROUND(u.distance),photo:u.photo,items:(FOR ui IN u.@items FOR i IN @@collection FILTER ui.aid == i._id RETURN i)}",
                GET_FULL_USER_BY_PROP: "FOR u IN @@collection FILTER u.@prop == @value RETURN MERGE(u, {email: FIRST((FOR e IN EMAILS_COLLECTION FILTER e.uid == u.id RETURN e)), commodities: (FOR uc IN u.commodities FOR c IN COMMODITIES_COLLECTION FILTER uc.aid == c._id RETURN c), demands: (FOR ud IN u.demands FOR d IN DEMANDS_COLLECTION FILTER ud.aid == d._id RETURN d), gifts: (FOR ug IN u.gifts FOR g IN GIFTS_COLLECTION FILTER ug.aid == g._id RETURN g), accounts: (FOR a IN ACCOUNTS_COLLECTION FILTER a.uid == u.id RETURN a)})",
                MODITY_OBJECT_PROPERTY: "FOR obj IN @@collection FILTER obj.@fprop == @fvalue UPDATE obj WITH @properties IN @@collection",
                SET_ALL_USERS_OFFLINE: "FOR u IN @@collection UPDATE u WITH {online:false} IN @@collection",
                LIST_MESSAGE_CODE: "FOR m IN @@collection FILTER m.phone == @phone && m.type == @type SORT m.timestamp DESC RETURN m",
                GET_SUMMARY: "RETURN { total: LENGTH((FOR u IN USERS_COLLECTION RETURN u)), online:LENGTH((FOR u IN USERS_COLLECTION FILTER u.online == true RETURN u))}"
            }
        }
    },
    errors: errors,
    regexps: {
        email: /.+@.+/,
        datetime_short: /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/,
        datetime_long: /\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}:\d{3}/
    },
    devices: {
        android: [],
        ios: [],
        web: []
    },
    image: {
        fullsize: {
            width: 480,
            height: 480
        },
        thumbsize: {
            width: 120,
            height: 120
        },
        userfullsize: {
            width: 180,
            height: 180
        }
    },
    routes: routes
};

module.exports = config;
